﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

using G.Runtime;

namespace Conquest
{
    public class PlayerAndCount : IComparable
    {
        public string name;
        public int count;
        
        int IComparable.CompareTo(object obj)
        {
            return (count - ((PlayerAndCount)obj).count);
        }
    }
        
    public class ScoreComputer
    {
        private StateMap _StateMap;
        private LaunchingArea _LaunchingArea;
        
        public ScoreComputer(
            StateMap pStateMap, LaunchingArea pLaunchingArea)
        {
            _StateMap = pStateMap;
            _LaunchingArea = pLaunchingArea;
        }
        
        public void AssignScores()
        {
            foreach (NamedCityState city in _StateMap.CityStates) 
            {
                List<PlayerAndCount> list = FindWinner(city);
                
                if (list.Count > 0)
                {
                    PlayerAndCount winner = list[0];
                    
                    if (winner.name == _LaunchingArea.PlayerPad1.PlayerName)
                    {
                        _LaunchingArea.PlayerPad1.Score += city.First;
                    }
                    
                    if (winner.name == _LaunchingArea.PlayerPad2.PlayerName)
                    {
                        _LaunchingArea.PlayerPad2.Score += city.First;
                    }
                }
                
                if (list.Count > 1)
                {
                    PlayerAndCount winner = list[1];
                    
                    if (winner.name == _LaunchingArea.PlayerPad1.PlayerName)
                    {
                        _LaunchingArea.PlayerPad1.Score += city.First;
                    }
                    
                    if (winner.name == _LaunchingArea.PlayerPad2.PlayerName)
                    {
                        _LaunchingArea.PlayerPad2.Score += city.First;
                    }
                }
                
                if (list.Count > 2)
                {
                    PlayerAndCount winner = list[2];
                    
                    if (winner.name == _LaunchingArea.PlayerPad1.PlayerName)
                    {
                        _LaunchingArea.PlayerPad1.Score += city.First;
                    }
                    
                    if (winner.name == _LaunchingArea.PlayerPad2.PlayerName)
                    {
                        _LaunchingArea.PlayerPad2.Score += city.First;
                    }
                }
            }
            
        }
        
        List<PlayerAndCount> FindWinner(NamedCityState city)
        {
            Dictionary<string, PlayerAndCount> map = 
                new Dictionary<string, PlayerAndCount>();
            
            foreach (GameElement element in city.CityState.Game.Elements) 
            {
                if (map.ContainsKey(element.Player)) 
                {
                    map[element.Player].count++;
                } 
                else
                {
                    PlayerAndCount p = new PlayerAndCount();
                    p.count = 1;
                    p.name = element.Player;
                    map[element.Player] = p;
                }
            }
            
            List<PlayerAndCount> list = new List<PlayerAndCount>();
            foreach (PlayerAndCount pc in map.Values) 
            {
                list.Add(pc);
            }
            
            list.Sort();
            return list;
        }
    }
}
